let num16:number[]=[0,0,0],target16:number=1;
console.log(threeSumClosest(num16,target16));

function threeSumClosest(nums: number[], target: number): number {
    let result:number=99999;
    //1、先排序;
    nums.sort((a, b) => a - b);//从小到大
    // console.log(nums);
    
    for(let i=0;i<nums.length-2;i++){
        let l=i+1,r=nums.length-1;
        while(l<r){
            if(nums[i]+nums[l]+nums[r]-target===0){
                result=nums[i]+nums[l]+nums[r];
                break
            }else if(nums[i]+nums[l]+nums[r]-target>0){
                if(Math.abs(nums[i]+nums[l]+nums[r]-target)<Math.abs(result-target))
                    result=nums[i]+nums[l]+nums[r];
                r--;
            } else{
                if(Math.abs(nums[i]+nums[l]+nums[r]-target)<Math.abs(result-target))
                    result=nums[i]+nums[l]+nums[r];
                l++
            }
        }
    }
    return result;
};